package com.ruoyi.zxjd.document.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zxjd.common.CommonConstants;
import com.ruoyi.zxjd.common.domain.CommonUserVO;
import com.ruoyi.zxjd.common.mapper.CommonMapper;
import com.ruoyi.zxjd.document.domain.DocumentArrange;
import com.ruoyi.zxjd.document.domain.DocumentProgress;
import com.ruoyi.zxjd.document.mapper.DocumentArrangeMapper;
import com.ruoyi.zxjd.document.service.IDocumentArrangeService;
import com.ruoyi.zxjd.document.service.IDocumentFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 公文管理Service业务层处理
 * 
 * @author lcc
 * @date 2021-01-28
 */
@Service
public class DocumentArrangeServiceImpl implements IDocumentArrangeService 
{
    @Autowired
    private CommonMapper commonMapper;
    @Autowired
    private DocumentArrangeMapper documentArrangeMapper;
    @Autowired
    private IDocumentFileService documentFileService;


    /**
     * 查询公文管理
     * 
     * @param arrangeId 公文管理ID
     * @return 公文管理
     */
    @Override
    public DocumentArrange selectDocumentArrangeById(Long arrangeId)
    {
        DocumentArrange documentArrange = documentArrangeMapper.selectDocumentArrangeById(arrangeId);
        String[] userIds = null;
        if(documentArrange.getTotal()>0){
            userIds = documentArrangeMapper.selectReceiveUserByArrangeId(arrangeId);
        }
        documentArrange.setUserIds(userIds);
        return documentArrange;
    }

    /**
     * 查询公文管理列表
     * 
     * @param documentArrange 公文管理
     * @return 公文管理
     */
    @Override
    public List<DocumentArrange> selectDocumentArrangeList(DocumentArrange documentArrange)
    {
        return documentArrangeMapper.selectDocumentArrangeList(documentArrange);
    }

    /**
     * 新增公文管理
     * 
     * @param documentArrange 公文管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDocumentArrange(DocumentArrange documentArrange)
    {
        documentArrange.setCreateBy(ShiroUtils.getLoginName());
        documentArrange.setCreateTime(DateUtils.getNowDate());
        documentArrange.setUserId(ShiroUtils.getUserId());
        documentArrange.setUserName(ShiroUtils.getSysUser().getUserName());
        documentArrange.setDeptId(ShiroUtils.getSysUser().getDeptId());
        documentArrange.setDeptName(ShiroUtils.getSysUser().getDept().getDeptName());
        int rows = documentArrangeMapper.insertDocumentArrange(documentArrange);
        insertDocumentProgress(documentArrange);
        String attachments = documentArrange.getAttachments();
        if(StringUtils.isNotEmpty(attachments)){
            updateBusinessByFileId(documentArrange.getArrangeId(),"公文",attachments);
        }
        return rows;
    }

    /**
     * 修改公文管理
     * 
     * @param documentArrange 公文管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDocumentArrange(DocumentArrange documentArrange)
    {
        documentArrange.setUpdateBy(ShiroUtils.getLoginName());
        documentArrange.setUpdateTime(DateUtils.getNowDate());
        String status = documentArrange.getStatus();
        if(StringUtils.equals(CommonConstants.DRAFT, status)){
            documentArrangeMapper.deleteDocumentProgressByArrangeId(documentArrange.getArrangeId());
            insertDocumentProgress(documentArrange);
        }
        return documentArrangeMapper.updateDocumentArrange(documentArrange);
    }

    /**
     * 删除公文管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDocumentArrangeByIds(String ids)
    {
        documentArrangeMapper.deleteDocumentProgressByArrangeIds(Convert.toStrArray(ids));
        documentArrangeMapper.deleteDocumentFileByArrangeIds(Convert.toStrArray(ids));
        return documentArrangeMapper.deleteDocumentArrangeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除公文管理信息
     * 
     * @param arrangeId 公文管理ID
     * @return 结果
     */
    @Override
    public int deleteDocumentArrangeById(Long arrangeId)
    {
        documentArrangeMapper.deleteDocumentProgressByArrangeId(arrangeId);
        documentArrangeMapper.deleteDocumentFileByArrangeId(arrangeId);
        return documentArrangeMapper.deleteDocumentArrangeById(arrangeId);
    }

    /**
     * 新增公文处理信息
     * 
     * @param documentArrange 公文管理对象
     */
    public void insertDocumentProgress(DocumentArrange documentArrange)
    {
        String[] userIds = documentArrange.getUserIds();
        String releaseScope = documentArrange.getReleaseScope();
        List<CommonUserVO> list = null;
        if(CommonConstants.AllUSER.equals(releaseScope)){
            list = commonMapper.listActiveUser(null,null);
        }else{
            list = commonMapper.listActiveUserByIds(userIds);
        }
        List<DocumentProgress> documentProgressList = new ArrayList<>();
        if(StringUtils.isNotNull(list)){
            for (CommonUserVO userVO : list) {
                DocumentProgress documentProgress = new DocumentProgress();
                documentProgress.setArrangeId(documentArrange.getArrangeId());
                documentProgress.setArrangeTitle(documentArrange.getArrangeTitle());
                documentProgress.setReceiveUserId(userVO.getUserId());
                documentProgress.setReceiveUser(userVO.getUserName());
                documentProgress.setReceiveDeptId(userVO.getDeptId());
                documentProgress.setReceiveDept(userVO.getDeptName());
                documentProgress.setEndDate(documentArrange.getEndDate());

                // 1 草稿,
                if (documentArrange.getStatus().equals("1")) {
                    documentProgress.setReleaseStatus(CommonConstants.UNPUBLISHED);
                // 2.发布, 他人能接收公文
                } else if(documentArrange.getStatus().equals("2")){
                    documentProgress.setReleaseStatus(CommonConstants.PUBLISHED);
                }

                documentProgress.setStatus(CommonConstants.TO_BE_RECEIVED);
                documentProgress.setCreateBy(ShiroUtils.getLoginName());
                documentProgress.setCreateTime(DateUtils.getNowDate());
                documentProgressList.add(documentProgress);
            }
        }
        if (documentProgressList.size() > 0)
        {
            documentArrangeMapper.batchDocumentProgress(documentProgressList);
        }
    }

    /**
     * 更新附件业务ID
     * @param businessId
     * @param businessType
     * @param fileIds
     * @return
     */
    public int updateBusinessByFileId(Long businessId, String businessType, String fileIds){
        return documentFileService.updateBusinessByFileId(businessId,businessType,Convert.toStrArray(fileIds));
    }

    /**
     * 通过工作安排ID修改工作安排状态
     * 
     * @param arrangeId 工作安排ID
     * @param status 工作状态（1草稿 2进行中 3已完成）
     * @return
     */
    @Override
    public int updateStatusByArrangeId(Long arrangeId, String status){
        String releaseStatus = status.equals(CommonConstants.DRAFT) ? CommonConstants.UNPUBLISHED:CommonConstants.PUBLISHED;
        documentArrangeMapper.updateReleaseStatusByArrangeId(arrangeId, releaseStatus);
        return documentArrangeMapper.updateStatusByArrangeId(arrangeId, status);
    }

}